package com.example.aidl;

import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log;

import com.example.aidlclient.BitMapIBinder;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AIDLService extends Service {
    private final String TAG = "AIDLService";
    private List<IBook> bookList;
   static BitmapCallback mBitmapCallback;
    public AIDLService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        bookList = new ArrayList<>();
        initData();
    }

    public static void setCallback(BitmapCallback bitmapCallback){
        mBitmapCallback =bitmapCallback;
    }
    private void initData() {

        for (int i = 0; i < 6; i++) {
            bookList.add(new IBook(i, "" + i));
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return stub;
    }

    BookController.Stub stub = new BookController.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public List<IBook> getBookList() throws RemoteException {
            Log.d(TAG, "getBookList");
            return bookList;
        }

        @Override
        public void addBookInOut(IBook book) throws RemoteException {
            Log.d(TAG, "addBookInOut");
            if (book != null) {
                bookList.add(book);
            } else {
                Log.d(TAG, "BOOK==null");
            }
        }

        @Override
        public void sendBitmap(Bundle bundle) throws RemoteException {
            Log.d(TAG, "sendBitmap");
            BitMapIBinder sjl =BitMapIBinder.Stub.asInterface(bundle.getBinder("sjl"));
            //BitMapIBinder.Stub sjl = (BitMapIBinder.Stub)bundle.getBinder("sjl");
            if (sjl!=null){
                int length = sjl.getBytes().length;
                Log.d(TAG, "sjl!=null 大小为: "+length);
              /*  int byteCount = sjl.getBitmap().getByteCount();
                mBitmapCallback.callBack(sjl.getBitmap());
                Log.d(TAG, "sjl!=null 大小为: "+byteCount);*/
            }else {
                Log.d(TAG, "sjl==null");
            }
        }

        @Override
        public void sendData(ParcelFileDescriptor pfd) throws RemoteException {
            Log.d(TAG, "sendData(ParcelFileDescriptor pfd)");
            FileDescriptor fileDescriptor = pfd.getFileDescriptor();
            FileInputStream fileInputStream = new FileInputStream(fileDescriptor);
            byte[] readData =new byte[1024];
            int len =0;
            while (true){
                try {
                    if (((len=fileInputStream.read(readData))!=-1)){
                       // Log.d(TAG, "本次读到的数据长度为："+len);
                        if (len!=1024){
                            Log.d(TAG, "本次读到的数据长度不是1024："+len);
                        }
                        StringBuffer stringBuffer = new StringBuffer();
                        for (int i = 0; i < len; i++) {
                            stringBuffer.append(i+" ");
                        }
                        //Log.d(TAG, stringBuffer.toString());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    };

    public interface BitmapCallback{
        void callBack(Bitmap bitmap);
    }

}
